#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 高管人员
爬取类目：高管人员
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/management/szmb000001.html
"""
import sys
import json
from xtls.logger import get_logger

from company_formaltable_crawler import CompanyFormalTable


reload(sys)
sys.setdefaultencoding('utf-8')
"""
result_sample_of_company_management = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'management':[{
        'name': '',
        'position': '',
        'birthYear': '',
        'gender': '',
        'degree': '',
    },],
}
"""

def get_detail_mapping():
    DETAIL_MAPPING = {
        u'姓名': 'name',
        u'职务': 'position',
        u'出生年份': 'birthYear',
        u'性别': 'gender',
        u'学历'	: 'degree',
    }
    return DETAIL_MAPPING

class CompanyManagement(CompanyFormalTable):
    def __init__(self, stock_list):
        logger = get_logger(__file__)
        super(CompanyManagement, self).__init__(stock_list=stock_list, logger=logger)
        self.category = 'management'
        self.tab_name = 'management'
        self.detail_mapping = get_detail_mapping()

    def clean_data(self, dirty_data):
        self.cleaner.set_dirty_data(dirty_data)
        self.cleaner.clean_get_id_list(['position'], conn=self.conn)
        self.cleaner.clean_get_degree_id(['degree'], conn=self.conn)
        return self.cleaner.complete()

    def save_update(self, data):
        manager_list = data.get('management')
        if not manager_list:
            self.logger.warn('management has no information %s-%s' % (data.get('stock_id'), data.get('stock_code')))
            return
        if not data.get('stock_id'):
            return

        version_num = 1 + self.select_current_version_num(table_name='management',
                                                          stock_id=data.get("stock_id"))
        for single in manager_list:
            single_clean = self.clean_data(single)
            single_clean['stockId'] = data.get('stock_id', None)
            single_clean['versionNum'] = version_num
            self.insert_to_db(table_name='management', cleaned_data=single_clean)

    def save_print(self, data):
        print json.dumps(data, indent=4)
        print "=" * 70


def main():
    pass

if __name__ == '__main__':
    main()